rm(list=setdiff(ls(), "twd")) 
library(fixest);library(AER);library(stringr);library(knitr)

data<-readRDS(paste(twd,"data/made_data/vreg_wgeos_race.rds",sep=""))

## In person 20
data$in_person_primary_20<-ifelse(data$voting_method_200303 == "P",1,0)
## Abs early 20
data$abs_early_primary_20<-ifelse(data$voting_method_200303 %in% c("A","E"),1,0)
## In person 18
data$in_person_general_18<-ifelse(data$voting_method_181106 == "P",1,0)
## Abs early 18
data$abs_early_general_18<-ifelse(data$voting_method_181106 %in% c("A","E"),1,0)
## Take First Difference
data$diff_in_person<-data$in_person_primary_20-data$in_person_general_18
data$diff_abs_early<-data$abs_early_primary_20-data$abs_early_general_18
### get the minimum of the distance to a consolidated (including super sites)
data$distance_realized_min<-apply(cbind(data$distance_realized,data$distancegov1,data$distancegov2),1,min,na.rm=T)
## Difference between assigned and realized distances (in logs and levels)
data$ln_dist_change<-ifelse(data$moved==1,log(data$distance_realized_min)-log(data$distance_assigned),0)
data$lev_dist_change<-ifelse(data$moved==1,data$distance_realized_min-data$distance_assigned,0)
### Difference between assigned and realized size # of assigned voters (in logs and levels)
data$ln_diff_N<-log(data$realized_N)-log(data$assigned_N)
data$lev_diff_N<-data$realized_N-data$assigned_N

#### Create differences in turnout for all previous elections and run regression ######

## 18-18b

data$in_person_primary_18<-ifelse(data$voting_method_180802== "P",1,0)
data$abs_early_primary_18<-ifelse(data$voting_method_180802 %in% c("A","E"),1,0)

data$diff_in_person_18_18<-data$in_person_general_18-data$in_person_primary_18
data$diff_abs_early_18_18<-data$abs_early_general_18-data$abs_early_primary_18

## 18-16

data$in_person_general_16<-ifelse(data$voting_method_161108== "P",1,0)
data$abs_early_general_16<-ifelse(data$voting_method_161108 %in% c("A","E"),1,0)

data$diff_in_person_18_16<-data$in_person_primary_18-data$in_person_general_16
data$diff_abs_early_18_16<-data$abs_early_primary_18-data$abs_early_general_16

## 16-16a

data$in_person_primary_16a<-ifelse(data$voting_method_160804== "P",1,0)
data$abs_early_primary_16a<-ifelse(data$voting_method_160804 %in% c("A","E"),1,0)

data$diff_in_person_16_16a<-data$in_person_general_16-data$in_person_primary_16a
data$diff_abs_early_16_16a<-data$abs_early_general_16-data$abs_early_primary_16a

## 16a-16b

data$in_person_primary_16b<-ifelse(data$voting_method_160301== "P",1,0)
data$abs_early_primary_16b<-ifelse(data$voting_method_160301 %in% c("A","E"),1,0)


data$diff_in_person_16_16b<-data$in_person_primary_16a-data$in_person_primary_16b
data$diff_abs_early_16_16b<-data$in_person_primary_16a-data$abs_early_primary_16b

## 16b-14

data$in_person_general_14<-ifelse(data$voting_method_141104== "P",1,0)
data$abs_early_general_14<-ifelse(data$voting_method_141104 %in% c("A","E"),1,0)

data$diff_in_person_16_14<-data$in_person_primary_16b-data$in_person_general_14
data$diff_abs_early_16_14<-data$in_person_primary_16b-data$abs_early_general_14

## 14-14a

data$in_person_primary_14<-ifelse(data$voting_method_140807== "P",1,0)
data$abs_early_primary_14<-ifelse(data$voting_method_140807 %in% c("A","E"),1,0)

data$diff_in_person_14_14<-data$in_person_general_14 - data$in_person_primary_14
data$diff_abs_early_14_14<-data$abs_early_general_14 - data$abs_early_primary_14

## 14a-12

data$in_person_general_12<-ifelse(data$voting_method_121106== "P",1,0)
data$abs_early_general_12<-ifelse(data$voting_method_121106 %in% c("A","E"),1,0)

data$diff_in_person_14_12<-data$in_person_primary_14 - data$in_person_general_12
data$diff_abs_early_14_12<-data$abs_early_primary_14 - data$abs_early_general_12



### Length of unique ops
lu<-function(x){length(unique(x))}


## Get  Dem votes
dem_votes<-function(x){ 
  sum( ifelse(str_trim(x) %in% c("DG","D"),1,0 ))
}

## Get  Rep votes
rep_votes<-function(x){ 
  sum( ifelse(str_trim(x) %in% c("RG","R"),1,0 ))
}

## Get All votes
total_votes<-function(x){
  sum(x)
}

## Get Dem/Rep Votes for each
data$dem_primary_votes<-apply(data[,c("party_voted_180802","party_voted_160804", "party_voted_160301","party_voted_140807")],1, dem_votes)
data$rep_primary_votes<-apply(data[,c("party_voted_180802","party_voted_160804", "party_voted_160301","party_voted_140807")],1, rep_votes)

## Absentee 
votes_abs<-c("abs_early_primary_18","abs_early_general_16","abs_early_primary_16a","abs_early_primary_16b","abs_early_general_14","abs_early_primary_14","abs_early_general_12")
## In Person
votes_in<-c("in_person_primary_18","in_person_general_16","in_person_primary_16a","in_person_primary_16b","in_person_general_14","in_person_primary_14","in_person_general_12")
## Absentee | Primary
primary_abs<-c("abs_early_primary_18","abs_early_primary_16a","abs_early_primary_16b","abs_early_primary_14")
## In Person | Primary
primary_in<-c("in_person_primary_18","in_person_primary_16a","in_person_primary_16b","in_person_primary_14")

### Get each of these for the individual
data$all_votes<-apply(data[,c(votes_abs,votes_in)],1, total_votes)
data$primary_votes<-apply(data[,c(primary_abs,primary_in)],1, total_votes)
data$general_votes<-data$all_votes- data$primary_votes


### Run Regressions

suppressMessages({ 
  
  ft1<-feols(diff_in_person~(moved+consolidated)*all_votes+I(dist_damage_min<500)+I(dist_power_min<500)+diff_abs_early|
               all_votes,data=data,cluster~polling_place_text_name)
  ft2<-feols(diff_in_person~(moved+consolidated)*general_votes+I(dist_damage_min<500)+I(dist_power_min<500)+diff_abs_early|
               general_votes,data=data,cluster~polling_place_text_name)
  ft3<-feols(diff_in_person~(moved+consolidated)*primary_votes+I(dist_damage_min<500)+I(dist_power_min<500)+diff_abs_early|
               primary_votes,data=data,cluster~polling_place_text_name)
})


pdf(paste(twd,"figs/marginal_effects_past.pdf",sep=""),18,6)


par(mfrow=c(1,3))


### Get Marginal Effect for all votes

newdata_1<-sort(unique(data$all_votes))
pred_1<-coef(ft1)[1]+ coef(ft1)[6]*newdata_1 
se_1<-sqrt(vcov(ft1)[1,1]+newdata_1^2*vcov(ft1)[6,6] + 2*vcov(ft1)[6,1]*newdata_1)

plot(newdata_1,pred_1,xlab="Past Turnout (All Elections)",ylim=c(-.275,.025),axes=F,type="n",ylab="Effect on in-Person Turnout",main="Total Past Turnout")
par(new=T)
barplot(table(data$all_votes),border = F,space=0,axes=F,names.arg=F)
par(new=T)
grid()
par(new=T)
plot(newdata_1,pred_1,xlab="",ylim=c(-.275,.025),axes=F,type="l",ylab="",main="",lwd=2)
lines(newdata_1,pred_1+1.96*se_1,lwd=2,lty=2)
lines(newdata_1,pred_1-1.96*se_1,lwd=2,lty=2)
abline(h=0,col="dark red",lty=4,lwd=2)
axis(1);axis(2)


### Get Marginal Effect for General votes


newdata_2<-sort(unique(data$general_votes))
pred_2<-coef(ft2)[1]+ coef(ft2)[6]*newdata_2 
se_2<-sqrt(vcov(ft2)[1,1]+newdata_2^2*vcov(ft2)[6,6] + 2*vcov(ft2)[6,1]*newdata_2)

plot(newdata_2,pred_2,xlab="Past Turnout (General Elections)",ylim=c(-.275,.025),axes=F,type="n",ylab="Effect on in-Person Turnout",main="General Past Turnout")
par(new=T)
barplot(table(data$general_votes),border = F,space=0,axes=F,names.arg=F)
par(new=T)
grid()
par(new=T)
plot(newdata_2,pred_2,xlab="",ylim=c(-.275,.025),axes=F,type="l",ylab="",main="",lwd=2)
lines(newdata_2,pred_2+1.96*se_2,lwd=2,lty=2)
lines(newdata_2,pred_2-1.96*se_2,lwd=2,lty=2)
abline(h=0,col="dark red",lty=4,lwd=2)
axis(1);axis(2)



### Get Marginal Effect for Primary votes


newdata_3<-sort(unique(data$primary_votes))
pred_3<-coef(ft3)[1]+ coef(ft3)[6]*newdata_3 
se_3<-sqrt(vcov(ft3)[1,1]+newdata_3^2*vcov(ft3)[6,6] + 2*vcov(ft3)[6,1]*newdata_3)

plot(newdata_3,pred_3,xlab="Past Turnout (Primary Elections)",ylim=c(-.275,.025),axes=F,type="n",ylab="Effect on in-Person Turnout",main="Primary Past Turnout")
par(new=T)
barplot(table(data$primary_votes),border = F,space=0,axes=F,names.arg=F)
par(new=T)
grid()
par(new=T)
plot(newdata_3,pred_3,xlab="",ylim=c(-.275,.025),axes=F,type="l",ylab="",main="",lwd=2)
lines(newdata_3,pred_3+1.96*se_3,lwd=2,lty=2)
lines(newdata_3,pred_3-1.96*se_3,lwd=2,lty=2)
abline(h=0,col="dark red",lty=4,lwd=2)
axis(1);axis(2)


dev.off()
